/**
 * 工艺路线管理
 */
Ext.define('YX.smfr.ProcessPath', {
	 extend : 'Ext.panel.Panel',
	 gridAllOpers : null,
	 btnAddOper : null,
	 btnRemoveOper : null,
	 btnOperUp : null,
	 btnOperDown : null,
	 gridPathOpers : null,
	 pelOper : null,
	 txtPathCode : null,
	 txtPathDesc : null,
	 cmbPathType : null,
	 txtMemo : null,
	 frmOperate : null,
	 txtFilterPath : null,
	 gridPath : null,
	 pelPath : null,
	 layout : 'border',
	 btnSave : null,
	 btnDelete : null,
	 initComponent : function() {
	  var allOperStore = Ext.create('Ext.data.JsonStore', {
		   fields : ['oper', 'operDesc'],
		   pageSize : 200,
		   autoLoad : true,
		   proxy : {
		    type : 'zbusrpc',
		    module : 'SmfrViewOperationService',
		    reader : {
			   root : 'operList'
		    }
		   }
	    });
	  this.gridAllOpers = Ext.create('Ext.grid.Panel', {
		   title : T.label.operation,
		   flex : 1,
		   columns : [{
		      text : T.label.operation,
		      dataIndex : 'oper',
		      width : 100
		     }, {
		      text : T.label.operationDesc,
		      dataIndex : 'operDesc',
		      width : 200
		     }],
		   store : allOperStore,
		   bbar : {
		    xtype : 'pagingtoolbar',
		    store : allOperStore
		   }
	    });

	  this.gridPathOpers = Ext.create('Ext.grid.Panel', {
		   title : T.label.pathOperation,
		   flex : 1,
		   columns : [{
		      text : T.label.operation,
		      dataIndex : 'oper',
		      width : 100
		     }, {
		      text : T.label.operationDesc,
		      dataIndex : 'operDesc',
		      width : 200
		     }],
		   store : Ext.create('Ext.data.JsonStore', {
		      fields : ['pathCode', 'oper', 'operDesc'],
		      pageSize : 100,
		      proxy : {
			     type : 'zbusrpc',
			     module : 'SmfrViewProcessPathOperationService',
			     reader : {
			      root : 'operList'
			     }
		      }
		     })
	    });

	  this.btnAddOper = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-add',
		   scope : this,
		   handler : this.onBtnAddOperClicked
	    });
	  this.btnRemoveOper = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-remove',
		   scope : this,
		   handler : this.onBtnRemoveOperClicked
	    });
	  this.btnOperUp = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-go-up',
		   scope : this,
		   handler : this.onBtnOperUpClicked
	    });
	  this.btnOperDown = Ext.create('Ext.button.Button', {
		   iconCls : 'icon-go-down',
		   scope : this,
		   handler : this.onBtnOperDownClicked
	    });

	  this.btnSave = Ext.create('Ext.button.Button', {
		   text : T.label.save,
		   itemId : 'btnSave',
		   iconCls : 'icon-save',
		   scope : this,
		   handler : this.onBtnSaveClicked
	    });
	  this.btnDelete = Ext.create('Ext.button.Button', {
		   text : T.label.del,
		   itemId : 'btnDelete',
		   iconCls : 'icon-delete',
		   scope : this,
		   handler : this.onBtnDeleteClicked
	    });

	  this.pelOper = Ext.create('Ext.panel.Panel', {
		   region : 'center',
		   layout : {
		    type : 'hbox',
		    align : 'stretch'
		   },
		   items : [this.gridAllOpers, {
		      xtype : 'toolbar',
		      vertical : true,
		      layout : {
			     type : 'vbox',
			     pack : 'center'
		      },
		      items : [this.btnAddOper, this.btnRemoveOper, this.btnOperUp, this.btnOperDown]
		     }, this.gridPathOpers],
		   bbar : {
		    xtype : 'toolbar',
		    items : ['->', this.btnSave, this.btnDelete]
		   }
	    });

	  //工艺路线维护和列表
	  this.txtPathCode = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.processPath,
		   allowBlank : false,
		   name : 'pathCode',
		   maxLength : 30,
		   anchor : '98%'
	    });
	  this.txtPathDesc = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.processPathDesc,
		   allowBlank : false,
		   name : 'pathDesc',
		   maxLength : 50,
		   anchor : '98%'
	    });
	  this.cmbPathType = Ext.create('Ext.form.field.ComboBox', {
		   fieldLabel : T.label.type,
		   editable : false,
		   anchor : '98%',
		   name : 'pathType',
		   valueField : 'key1',
		   displayField : 'data1',
		   store : Ext.create('Ext.data.JsonStore', {
		      fields : ['key1', 'data1'],
		      proxy : {
			     type : 'zbusrpc',
			     module : 'BasViewCommonDataSetupDetailService',
			     extraParams : {
			      groupName : 'PATH_TYPE'
			     },
			     reader : {
			      root : 'cdsDtlList'
			     }
		      }
		     })
	    });
	  this.txtMemo = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.memo,
		   name : 'memo',
		   maxLength : 100,
		   anchor : '98%'
	    });
	  this.frmOperate = Ext.create('Ext.form.Panel', {
		   title : T.label.operate,
		   items : [this.txtPathCode, this.txtPathDesc, this.cmbPathType, this.txtMemo]
	    });

	  this.txtFilterPath = Ext.create('Ext.form.field.Text', {
		   fieldLabel : T.label.processPath,
		   emptyText : T.label.filter,
		   listeners : {
		    scope : this,
		    specialkey : this.onTxtFilterPathSpecialKey
		   }
	    });

	  var pathStore = Ext.create('Ext.data.JsonStore', {
		   autoLoad : true,
		   fields : ['pathCode', 'pathDesc', 'pathType', 'pathStatus', 'memo'],
		   proxy : {
		    type : 'zbusrpc',
		    module : 'SmfrViewProcessPathService',
		    reader : {
			   root : 'pathList'
		    }
		   }
	    });
	  this.gridPath = Ext.create('Ext.grid.Panel', {
		   title : T.label.processPath,
		   flex : 1,
		   viewConfig : {
		    getRowClass : function(record, rowIndex, rowParams, store) {
			   if (record.get('pathStatus') == 'D') {
			    return 'red-row';
			   }
		    }
		   },
		   columns : [{
		      text : T.label.processPath,
		      dataIndex : 'pathCode',
		      width : 120
		     }, {
		      text : T.label.processPathDesc,
		      dataIndex : 'pathDesc',
		      flex : 1
		     }],
		   store : pathStore,
		   bbar : {
		    xtype : 'pagingtoolbar',
		    store : pathStore,
		    displayInfo : true
		   },
		   listeners : {
		    scope : this,
		    select : this.onGridPathSelect
		   }
	    });

	  this.items = [this.pelOper, {
		   region : 'east',
		   split : true,
		   width : 300,
		   layout : {
		    type : 'vbox',
		    align : 'stretch'
		   },
		   items : [this.frmOperate, this.gridPath, this.txtFilterPath]
	    }];

	  this.callParent();
	 },
	 onBtnAddOperClicked : function(btn) {
	  var fromRcds = this.gridAllOpers.getSelectionModel().getSelection();
	  if (Ext.isEmpty(fromRcds)) {
		 return;
	  }
	  var vPathCode = this.txtPathCode.getValue().trim();
	  if (vPathCode === '') {
		 return;
	  }

	  var rcds = this.gridPathOpers.getStore().add({
		   pathCode : vPathCode,
		   oper : fromRcds[0].get('oper'),
		   operDesc : fromRcds[0].get('operDesc')
	    });
	  this.gridPathOpers.getSelectionModel().select(rcds);
	 },
	 onBtnRemoveOperClicked : function(btn) {
	  var rcds = this.gridPathOpers.getSelectionModel().getSelection();
	  if (!Ext.isEmpty(rcds)) {
		 this.gridPathOpers.getStore().remove(rcds);
	  }
	 },
	 onBtnOperUpClicked : function(btn) {
	  var rcds = this.gridPathOpers.getSelectionModel().getSelection();
	  if (!Ext.isEmpty(rcds)) {
		 var index = this.gridPathOpers.getStore().indexOf(rcds[0]);
		 if (index > 0) {
		  var nRcd = rcds[0].copy();
		  nRcd = this.gridPathOpers.getStore().insert(index - 1, nRcd);
		  this.gridPathOpers.getStore().remove(rcds[0]);
		  this.gridPathOpers.getSelectionModel().select(nRcd, false, true);
		 }
	  }
	 },
	 onBtnOperDownClicked : function(btn) {
	  var rcds = this.gridPathOpers.getSelectionModel().getSelection();
	  if (Ext.isEmpty(rcds)) {
		 return;
	  }

	  var dtlStore = this.gridPathOpers.getStore();
	  var index = dtlStore.indexOf(rcds[0]);
	  if (index < dtlStore.getCount() - 1) {
		 var nRcd = rcds[0].copy();
		 nRcd = dtlStore.insert(index + 2, nRcd);
		 dtlStore.remove(rcds[0]);
		 this.gridPathOpers.getSelectionModel().select(nRcd, false, true);
	  }
	 },
	 onBtnSaveClicked : function(btn) {
	  if (!this.frmOperate.getForm().isValid()) {
		 return;
	  }

	  var me = this;
	  Ext.Msg.show({
		   title : T.label.save,
		   msg : T.msg.are_you_sure_to_save_data,
		   icon : Ext.Msg.QUESTION,
		   buttons : Ext.Msg.YESNO,
		   fn : function(btnId) {
		    if (btnId == 'yes') {
			   btn.disable();

			   var parm = me.frmOperate.getValues();
			   var i = 1;
			   if (me.gridPathOpers.getStore().getCount() > 0) {
			    parm.operList = [];
			    me.gridPathOpers.getStore().each(function(rcd) {
				     parm.operList.push({
				        seqNum : i,
				        oper : rcd.get('oper'),
				        operDesc : rcd.get('operDesc')
				       });
				     i++;
			      });
			   }

			   YX.ZbusRpc.request({
			      module : 'SmfrSaveProcessPathService',
			      params : parm,
			      showSuccess : true,
			      callback : function() {
				     btn.enable();
			      },
			      success : function() {
				     me.frmOperate.getForm().reset();
				     me.gridPath.getStore().loadPage(1);
			      }
			     });
		    }
		   }
	    });
	 },
	 onBtnDeleteClicked : function(btn) {
	  if (!this.txtPathCode.isValid()) {
		 return;
	  }

	  var me = this;
	  Ext.Msg.show({
		   title : T.label.save,
		   msg : T.msg.are_you_sure_to_delete_data,
		   icon : Ext.Msg.QUESTION,
		   buttons : Ext.Msg.YESNO,
		   fn : function(btnId) {
		    if (btnId == 'yes') {
			   btn.disable();

			   YX.ZbusRpc.request({
			      module : 'SmfrDeleteProcessPathService',
			      showSuccess : true,
			      params : {
				     pathCode : me.txtPathCode.getValue()
			      },
			      callback : function() {
				     btn.enable();
			      },
			      success : function() {
				     me.frmOperate.getForm().reset();
				     me.gridPath.getStore().loadPage(1);
			      }
			     });
		    }
		   }
	    });
	 },
	 onGridPathSelect : function(sm, record) {
	  this.gridPathOpers.getStore().load({
		   params : {
		    pathCode : record.get('pathCode')
		   }
	    });
	  this.frmOperate.loadRecord(record);
	 },
	 onTxtFilterPathSpecialKey : function(field, e) {
	  if (e.getKey() == e.ENTER) {
		 this.gridPath.getStore().load({
		    params : {
			   pathCode : field.getValue()
		    }
		   });
	  }
	 }
  });